package com.etone.smartAudit.onlyoffice.util;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.excel.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.etone.smartAudit.commService.CommPUumUserinfoService;
import com.etone.smartAudit.domain.*;
import com.etone.smartAudit.dto.wordOrder.ImWorkOrderDTO;
import com.etone.smartAudit.mapper.AdminMapper;
import com.etone.smartAudit.mapper.ItemMapper;
import com.etone.smartAudit.mapper.ItemTypeMapper;
import com.etone.smartAudit.mapper.WorkOrderMapper;
import com.etone.smartAudit.service.PUumUserinfoService;
import com.etone.smartAudit.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;

import java.util.Date;
import java.util.List;

/**
 * @author 张任豪
 */
@Slf4j
public class WorkOrderListener implements ReadListener<ImWorkOrderDTO> {

    /**
     * 每隔2000条存储数据库，然后清理list ，方便内存回收
     */

    private AdminMapper adminMapper;

    private CommPUumUserinfoService pUumUserinfoService;

    private ItemMapper itemMapper;

    private ItemTypeMapper itemTypeMapper;


    public WorkOrderListener(CommPUumUserinfoService pUumUserinfoService, AdminMapper adminMapper, ItemMapper itemMapper, ItemTypeMapper itemTypeMapper) {
        this.adminMapper = adminMapper;
        this.pUumUserinfoService = pUumUserinfoService;
        this.itemMapper = itemMapper;
        this.itemTypeMapper = itemTypeMapper;
    }

    @Override
    public void invoke(ImWorkOrderDTO workOrderDTO, AnalysisContext analysisContext) {
//        WorkOrder workOrder = new WorkOrder();
        try {
            //处理项目名称
            Item item = itemMapper.selectOne(new QueryWrapper<Item>().eq("vcName", workOrderDTO.getItemName()));
            workOrderDTO.setItemId(item.getId());

            ItemType itemType = itemTypeMapper.selectOne(new QueryWrapper<ItemType>().eq("vcName", workOrderDTO.getItemTypeName()));
            workOrderDTO.setItemType(itemType.getId());

            //处理工单责任人
            PUumUserinfo userinfo = pUumUserinfoService.getOne(new QueryWrapper<PUumUserinfo>()
                    .eq(workOrderDTO.getAuditUnit() != null, "company", workOrderDTO.getAuditUnit())
                    .eq(workOrderDTO.getWordOrderName() != null, "fullName", workOrderDTO.getWordOrderName()));
            QueryWrapper<Admin> wrapper = new QueryWrapper<>();
            wrapper.eq("vcAccount", userinfo.getUserID());
            Admin admin = adminMapper.selectOne(wrapper);
            workOrderDTO.setWordOrderId(admin.getId());

            //处理审计组联系人
            PUumUserinfo pUumUserinfo = pUumUserinfoService.getOne(new QueryWrapper<PUumUserinfo>()
                    .eq(workOrderDTO.getAuditUnit() != null, "company", "省公司")
                    .eq(workOrderDTO.getWordOrderName() != null, "fullName", workOrderDTO.getAuditGroup()));
            workOrderDTO.setAuditGroupAccount(pUumUserinfo.getUserID());
            workOrderDTO.setIsError(false);
            workOrderDTO.setMassage("success");
        } catch (Exception e) {
            workOrderDTO.setIsError(true);
            workOrderDTO.setMassage("工单信息有误，请检查");
        }

    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
//        workOrderMapper.saveAll(cachedDataList);
    }
}
